{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn import preprocessing\n",
    "# 数据是否需要标准化\n",
    "scale = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 载入数据\n",
    "data = np.genfromtxt(\"LR-testSet.csv\", delimiter=\",\")\n",
    "x_data = data[:,:-1]\n",
    "y_data = data[:,-1]\n",
    "    \n",
    "def plot():\n",
    "    x0 = []\n",
    "    x1 = []\n",
    "    y0 = []\n",
    "    y1 = []\n",
    "    # 切分不同类别的数据\n",
    "    for i in range(len(x_data)):\n",
    "        if y_data[i]==0:\n",
    "            x0.append(x_data[i,0])\n",
    "            y0.append(x_data[i,1])\n",
    "        else:\n",
    "            x1.append(x_data[i,0])\n",
    "            y1.append(x_data[i,1])\n",
    "\n",
    "    # 画图\n",
    "    scatter0 = plt.scatter(x0, y0, c='b', marker='o')\n",
    "    scatter1 = plt.scatter(x1, y1, c='r', marker='x')\n",
    "    #画图例\n",
    "    plt.legend(handles=[scatter0,scatter1],labels=['label0','label1'],loc='best')\n",
    "    \n",
    "plot()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(100, 2)\n",
      "(100, 1)\n",
      "(100, 3)\n"
     ]
    }
   ],
   "source": [
    "# 数据处理，添加偏置项\n",
    "x_data = data[:,:-1]\n",
    "y_data = data[:,-1,np.newaxis]\n",
    "\n",
    "print(np.mat(x_data).shape)\n",
    "print(np.mat(y_data).shape)\n",
    "# 给样本添加偏置项\n",
    "X_data = np.concatenate((np.ones((100,1)),x_data),axis=1)\n",
    "print(X_data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def sigmoid(x):\n",
    "    return 1.0/(1+np.exp(-x))\n",
    "\n",
    "def cost(xMat, yMat, ws):\n",
    "    left = np.multiply(yMat, np.log(sigmoid(xMat*ws)))\n",
    "    right = np.multiply(1 - yMat, np.log(1 - sigmoid(xMat*ws)))\n",
    "    return np.sum(left + right) / -(len(xMat))\n",
    "\n",
    "def gradAscent(xArr, yArr):\n",
    "    \n",
    "    if scale == True:\n",
    "        xArr = preprocessing.scale(xArr)\n",
    "    xMat = np.mat(xArr)\n",
    "    yMat = np.mat(yArr)\n",
    "    \n",
    "    lr = 0.001\n",
    "    epochs = 10000\n",
    "    costList = []\n",
    "    # 计算数据行列数\n",
    "    # 行代表数据个数，列代表权值个数\n",
    "    m,n = np.shape(xMat)\n",
    "    # 初始化权值\n",
    "    ws = np.mat(np.ones((n,1)))\n",
    "    \n",
    "    for i in range(epochs+1):             \n",
    "        # xMat和weights矩阵相乘\n",
    "        h = sigmoid(xMat*ws)   \n",
    "        # 计算误差\n",
    "        ws_grad = xMat.T*(h - yMat)/m\n",
    "        ws = ws - lr*ws_grad \n",
    "        \n",
    "        if i % 50 == 0:\n",
    "            costList.append(cost(xMat,yMat,ws))\n",
    "    return ws,costList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.        ]\n",
      " [ 0.30816757]\n",
      " [-1.76171512]]\n"
     ]
    }
   ],
   "source": [
    "# 训练模型，得到权值和cost值的变化\n",
    "ws,costList = gradAscent(X_data, y_data)\n",
    "print(ws)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "if scale == False:\n",
    "    # 画图决策边界\n",
    "    plot()\n",
    "    x_test = [[-4],[3]]\n",
    "    y_test = (-ws[0] - x_test*ws[1])/ws[2]\n",
    "    plt.plot(x_test, y_test, 'k')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEWCAYAAACJ0YulAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3Xl8VNX9//HXJwRkUVkkorIIKqKo\nKBqsSwWXutal+kMrilsRRK0o1lbUumv7VbRVq1XRUmxF1LogRQEtri1UDSoom0WQRQQiiKK2sn1+\nf5ybMAkhmSQzczMz7+fjcR8zc+fM5HO5wDv3nnvONXdHREQEoCDuAkREpOFQKIiISDmFgoiIlFMo\niIhIOYWCiIiUUyiIiEg5hYJIGplZIzP7xsw6xV2LSDJM4xRENjGzbxJeNge+BzZEry9y99GZr0ok\ncxQKIltgZp8CF7r7P6ppU+ju6zNXlUh66fSRSC2Y2W1m9pSZjTGzNUB/MzvYzP5tZqvN7HMzu8/M\nGkftC83Mzaxz9Prx6P0JZrbGzKaaWZcYN0mkAoWCSO2dCjwBtASeAtYDlwNtgUOB44CLqvn8WcD1\nQBtgEXBrOosVqQ2Fgkjt/dPd/+7uG939v+7+rru/7e7r3X0+MALoU83nn3H3EndfB4wG9stI1SJJ\nKIy7AJEstDjxhZntAdwNHEDonC4E3q7m88sSnn8HbJ3qAkXqSkcKIrVX+eqMh4GPgN3cfVvgBsAy\nXpVICigUROpvG+Ar4Fsz25Pq+xNEGjSFgkj9/QI4D1hDOGp4Kt5yROpO4xRERKScjhRERKScQkFE\nRMopFEREpJxCQUREymXd4LW2bdt6586d4y5DRCSrTJs27Qt3L6qpXdaFQufOnSkpKYm7DBGRrGJm\nC5Npp9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N/DN6Phm4\nGMrvK73tlr7UzAqAju7+GuEmQq2AzY5WRDJFv5GIbNLMzD5IeD3R3csuS93KzN4m/CLVL1o3BBhp\nZr8k3CXtgmj95cAIMxtAOCK4mDArZlUaAY+bWUvCjJi/d/fVKdsikVpSn4JIDaI+hWJ3/yLuWkTS\nTaePRESknI4URESknI4URESknEJBRETKKRRERKScQkFERMopFEREpNz/B+w6yK2FiLe0AAAAAElF\nTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x21f31541d68>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画图 loss值的变化\n",
    "x = np.linspace(0,10000,201)\n",
    "plt.plot(x, costList, c='r')\n",
    "plt.title('Train')\n",
    "plt.xlabel('Epochs')\n",
    "plt.ylabel('Cost')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             precision    recall  f1-score   support\n",
      "\n",
      "        0.0       0.92      1.00      0.96        47\n",
      "        1.0       1.00      0.92      0.96        53\n",
      "\n",
      "avg / total       0.96      0.96      0.96       100\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 预测\n",
    "def predict(x_data, ws):\n",
    "    if scale == True:\n",
    "        x_data = preprocessing.scale(x_data)\n",
    "    xMat = np.mat(x_data)\n",
    "    ws = np.mat(ws)\n",
    "    return [1 if x >= 0.5 else 0 for x in sigmoid(xMat*ws)]\n",
    "\n",
    "predictions = predict(X_data, ws)\n",
    "\n",
    "print(classification_report(y_data, predictions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
